﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PS3Helpers.UserControls
{
    public partial class ContentIdEditor : UserControl
    {
        private bool _lockChangedEvent = false;

        public ContentIdEditor()
        {
            InitializeComponent();

            textBoxPart1.TextChanged += textBoxTextChanged;
            textBoxPart2.TextChanged += textBoxTextChanged;
            textBoxPart3.TextChanged += textBoxTextChanged;
            textBoxPart4.TextChanged += textBoxTextChanged;
        }

        private void textBoxTextChanged(object sender, EventArgs e)
        {
            NotifyContentIdChanged();
        }

        public event EventHandler ContentIdChanged;
        internal void NotifyContentIdChanged()
        {
            if (_lockChangedEvent)
                return;

            EventHandler handler = ContentIdChanged;

            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }

        public override string Text
        {
            get
            {
                return textBoxPart1.Text + "-" +
                       textBoxPart2.Text + "_" +
                       textBoxPart3.Text + "-" +
                       textBoxPart4.Text;

            }
            set
            {
                _lockChangedEvent = true;

                try
                {
                    string[] parts = value.Split('-', '_');

                    if (parts.Length == 4)
                    {
                        textBoxPart1.Text = parts[0];
                        textBoxPart2.Text = parts[1];
                        textBoxPart3.Text = parts[2];
                        textBoxPart4.Text = parts[3];
                    }
                    else
                        if (parts.Length == 1 && parts[0].Length == 9)
                        {
                            textBoxPart1.Text = "";
                            textBoxPart2.Text = parts[0];
                            textBoxPart3.Text = "";
                            textBoxPart4.Text = "";
                        }
                        else
                            if (string.IsNullOrEmpty(value))
                            {
                                textBoxPart1.Text = "";
                                textBoxPart2.Text = "";
                                textBoxPart3.Text = "";
                                textBoxPart4.Text = "";
                            }
                }
                finally
                {
                    _lockChangedEvent = false;
                }
            }
        }

        private void buttonAutoGenerateId_Click(object sender, EventArgs e)
        {
            Text = GenerateId();
            NotifyContentIdChanged();
        }

        public static string GenerateId()
        {
            string uniqueid = Guid.NewGuid().ToString("N");

            return uniqueid.Substring(0, 6) + "-" +
                   uniqueid.Substring(6, 9) + "_" +
                   "00" + "-" + // Must be "00"
                   "PS3SCENE" + uniqueid.Substring(15, 8);
        }
    }
}
